<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use think\Db;
use app\common\model\Project as Project_Model;
use app\web\validate\Project as Project_Validate;

/**
 * 项目分类控制器
 * @package app\index\controller
 */
class ProjectCate extends Auth
{
    public $noNeedLogin = '';


    /**
     *  获取项目分类
     */
    public function index(){
        $data = Db::name('qt_project_category')
            ->field('id,name,sort,pid')
            ->where(['store_id'=>$this->userInfo['store_id'],'status'=>'1'])
            ->where('pid','<>','0')
            ->select();
        foreach ($data as $key=>$value){
            $data[$key]['pid_name'] = Db::name('qt_project_category')->where('id',$value['pid'])->value('name');
        }
        $this->result('查询成功', $data, 200);
    }


    /**
     * 添加项目分类
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['sort']) || empty($getData['name']) || empty($getData['pid'])){
                $this->result('缺少参数', '', 204);
            }
            $getData['create_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            Db::name('qt_project_category')->insert($getData);
            $this->result('添加成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改项目分类
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['sort']) || empty($getData['name']) || empty($getData['pid']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            Db::name('qt_project_category')->where('id',$getData['id'])->update($getData);
            $this->result('修改成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除项目分类
     */
    public function del(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $projectFind  = Db::name('qt_project')->where(['project_category_id'=>$getData['id']])->find();
            if(!empty($projectFind)){
                $this->result('该分类下面有项目，禁止删除', '', 204);
            }
            Db::name('qt_project_category')->where('id',$getData['id'])->update(['status'=>'2']);
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }

}
